#!/bin/bash
# 计算 120 秒内 $1读写速率

if [ ! -f /proc/net/dev ]; then
  echo "Error: /proc/net/dev not exist"
  exit 1
fi

INTERVAL=120 # 批次时间
BYTE_TO_BIT=8

read rx_init tx_init < <(grep $1 /proc/net/dev | awk '{print $2, $10}')

sleep ${INTERVAL}s

read rx_end tx_end < <(grep $1 /proc/net/dev | awk '{print $2, $10}')

now=$(date '+%Y-%m-%d %H:%M:%S')

rx_diff=$((rx_end - rx_init))
tx_diff=$((tx_end - tx_init))
rx_rate=$(echo "scale=4; $rx_diff / $INTERVAL* $BYTE_TO_BIT / 1024" | bc)
tx_rate=$(echo "scale=4; $tx_diff / $INTERVAL* $BYTE_TO_BIT / 1024" | bc)

echo $now
echo "RX: $rx_rate kb/s"
echo "TX: $tx_rate kb/s"

